home *** CD-ROM | disk | FTP | other *** search
- Path: news.wright.edu!emoyer
- From: emoyer@cs.wright.edu (Eric Moyer)
- Newsgroups: comp.lang.c++
- Subject: Re: Random numbers in C++
- Date: 24 Jan 1996 19:58:24 GMT
- Organization: Wright State University, Dayton, OH 45435
- Message-ID: <4e6310$2hn@mercury.wright.edu>
- References: <4e07ge$me7@server1.ctc.com> <310523E6.624C@ntsrv.capacity.dk>
- NNTP-Posting-Host: gamma.cs.wright.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Frank Nielsen (frank.nielsen@ntsrv.capacity.dk) wrote:
- >Richard Kemp wrote:
- >>
- >> Hi!!
- >>
- >> How do you make a random number in C++. I'm not familiar with C, so if it's a C
- >> function that will help a lot also. Thanks!!
- >>
- >> --
- >> Richard Kemp
-
-
- >Hey Richard.
-
- >First call "C" function randomize(); at startup.
-
- >Then every time you need a random number you call random(<max random
- >number>).
-
-
- If you're not using DOS, the following calls will work:
-
- #include <time.h>
- #include <stdlib.h>
- #include <stdio.h>
-
- main(){
- int i;
- srand(time(0));
-
- /*To get a random number in range 0..9
- *Note that because of the % distribution is not perfect, but it's
- *close enough. If you need better distribution.
- *(10*rand())/RAND_MAX will work*/
-
- for (i=0;i<10;++i)
- printf("Random Number from 0..9: %d",rand()%10);
-
- return(0);
- };
-
- /*Better versions of rand are probably available as random() .. check your
- manual/manpages*/
- --
- -------------------------------------__|__-----------------------------------
- "A wise man once said nothing" | Emoyer@valhalla.cs.wright.edu
- ---------------------------------------| ------------------------------------
- GCS/M d(-+) p++@ c++++ l+ u++ e+(*) m++@ s+/ !n h--(*) f+ g+ w+++ t@ r+@ !y
-